<template>
  <div class="shipper-main">
    <div class="shi-header">
      <el-button type="primary"  @click="goShipperList">快递公司列表</el-button>
    </div>
    <div class="sender">
      <el-form ref="form" :model="infoForm" label-width="120px">
        <el-form-item label="打印后自动发货">
          <el-switch v-model="infoForm.autoDelivery"
            @change='changeStatus(infoForm.autoDelivery)'></el-switch>
        </el-form-item>
        <el-form-item label="寄件人" prop="Name">
            <el-input v-model="infoForm.Name" placeholder="请输入非代理发货时的寄件人"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="Tel">
            <el-input v-model="infoForm.Tel" placeholder="请输入电话"></el-input>
        </el-form-item>
        <el-form-item label="省份" prop="ProvinceName">
            <el-cascader
                    :options="options"
                    placeholder="请选择地区"
                    v-model="senderOptions">
            </el-cascader>
        </el-form-item>
        <el-form-item label="地址" prop="Address">
            <el-input v-model="infoForm.Address" placeholder="请输入具体地址"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSaveSubmit">确定保存</el-button>
        </el-form-item>
      </el-form>
    </div>



    <div class="form-table-box">
      <el-form label-width="120px">
          <el-form-item label="使用中的快递">
              <el-table :data="tableData" style="width: 100%" border stripe>
                  <el-table-column prop="name" label="名字"></el-table-column>
                  <el-table-column prop="code" label="代号"></el-table-column>
                  <el-table-column prop="customerName" label="客户编号"></el-table-column>
                  <el-table-column prop="monthCode" label="月结账号"></el-table-column>
                  <el-table-column label="操作" width="170">
                      <template #default="scope">
                          <el-button size="small" @click="handleRowEdit(scope.$index, scope.row)">编辑
                          </el-button>
                      </template>
                  </el-table-column>
              </el-table>
          </el-form-item>
      </el-form>
  </div>
  </div>
</template>
<script>
import { getAllRegion} from  '@/api/order.js'
import { getSenderData, getShipperListByQuery,saveSenderData} from  '@/api/shipper.js'
export default {
  data(){
    return {
      infoForm: {},
      tableData: [],
      options: [],
      senderOptions:[],
      infoRules: {
          Name: [
              {required: true, message: '请输入寄件人姓名', trigger: 'blur'},
          ],
          Tel: [
              {required: true, message: '请输入寄件人电话', trigger: 'blur'},
          ],
          ProvinceName: [
              {required: true, message: '请输入寄件省份', trigger: 'blur'},
          ],
          CityName: [
              {required: true, message: '请输入寄件城市', trigger: 'blur'},
          ],
          ExpAreaName: [
              {required: true, message: '请输入寄件区县', trigger: 'blur'},
          ],
          Address: [
              {required: true, message: '请输入寄件人具体地址', trigger: 'blur'},
          ],

      }
    }
  },
  methods:{
    changeStatus(autoDelivery){

    },
    async getAllRegion() {  
      let res = await getAllRegion()
      if(res.code===0){
        this.options = res.data
      }
      console.log('res:',res)
    },
    goShipperList(){
      this.$router.push('/setting/shipperList')
    },
    // 获取发件人
    async getSenderData(){
      let res = await getSenderData()
      if(res.code === 0){
        this.infoForm = res.data[0]
        this.senderOptions = [
          this.infoForm.province_id,
          this.infoForm.city_id,
          this.infoForm.district_id
        ]
        this.infoForm.autoDelivery = this.infoForm.autoDelivery === 1 ? true :false
      }
    },
    
    // 根据查询条件获取快递列表
    async getShipperListByQuery(){
      let res = await getShipperListByQuery({
        enabled:1
      })
      if(res.code === 0){
        this.tableData = res.data
      }
    },
    handleRowEdit(index,row){
      this.$router.push({ name: 'addShipper', query: { id: row.id } })
    },
    async onSaveSubmit(){
      let res = await saveSenderData(this.infoForm)
      if(res.code === 0){
        this.$message({
          type:'success',
          message:'保存成功'
        })
      }
    }
  },
  mounted() {
    // this.getList();
    this.getAllRegion();
    this.getSenderData()
    this.getShipperListByQuery()
  }
}
</script>
<style lang="scss" scoped>
.shi-header{
  text-align: right;
}
.sender{
  margin-top:20px;
  padding:20px;
  border:1px solid #eee;
  .el-form{
    width: 600px;
    ::v-deep .el-form-item__content{
      text-align: left;
    }
  }
}
.form-table-box{
  border:1px solid #eee;
  margin-top:20px;
  padding:20px;
}
</style>

